const mongoose = require("mongoose");
const uri =
  "mongodb+srv://2359647500:IqLqDFKyx0vH4UDj@brother.1z7ds.mongodb.net/Data?retryWrites=true&w=majority";

mongoose
  .connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log("连接成功");
  })
  .catch((err) => {
    console.log(err, "---连接失败---");
  });

const UserSchema = new mongoose.Schema({
  user: String,
  password: String,
  phone: String,
  head: String,
});

const UserModule = mongoose.model("peopleUser", UserSchema, "peopleUser");
// 商品列表
const GoodsSchema = new mongoose.Schema({
  name: String, //名称
  price: Number, //价钱
  num: Number, //数量
  img: String, //图片
  count: Number, //库存
  isshop: Boolean, //是否加入购物车
  isbuy: Boolean, //是否付款
  type: Number, //关联商家
  issale: Boolean, //是否在售
  tid: {
    type: mongoose.Types.ObjectId,
    ref: "type",
  }, //关联类型
  isgroup: Boolean, //是否团购
  iscollection: Boolean, //是否收藏
});
const GoodModel = mongoose.model("good", GoodsSchema, "good");
//商品类型列表
const TypeSchema = new mongoose.Schema({
  name: String, //类型名称
});
const TypeModel = mongoose.model("type", TypeSchema, "type");
//商家列表
const BusinessSchema = new mongoose.Schema({
  name: String, //商家名称
  type: Number, //关联商品类型
  status: Boolean, //商家状态,
  img1: String, //商家店铺环境图片1
  coupon: Number, //商家优惠卷
  img2: String, //商家店铺环境图片2
  gonggao: String, //商家公告,
  total: Number, //商家存款
  record: Array, //商家存款记录
});
const BusinessModel = mongoose.model("business", BusinessSchema, "business");

// 刘旭
// order
const OrderChema = new mongoose.Schema({
  order_time: {
    type: Date,
    default: Date.now,
  }, // 订单时间
  acceptance_time: {
    type: Date,
    default: Date.now,
  }, // 接单时间
  order_Content: Array, // 订单内容
  //订单内容
  order_id: String, // 订单号
  order_status: Number, // 订单状态 0已接单 1已完成 2配送中 3交易成功 4已退款 5已取消
  shop_name: String, // 商家名称
  oid: {
    type: mongoose.Schema.Types.ObjectId,
  }, // 商家id
});

// 聊天模型
const ChatChema = new mongoose.Schema({
  user: String, // 用户名称
  content: String, // 聊天内容
  time: {
    type: Date,
    default: Date.now,
  }, // 聊天时间
  is_read: {
    type: Boolean,
    default: false,
  }, // 是否已读
  type: String,
  uid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "use-shop",
  }, // 用户id
  tid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "shop",
  }, // 商家id
  chat_img: String, // 聊天图片
});

// 商家模型
const ShopChema = new mongoose.Schema({
  shop_name: String, // 商家名称
  shop_phone: String, // 商家电话
  shop_img: String, // 商家图片
});

// 用户模型
const UseChema = new mongoose.Schema({
  user_name: String, // 用户名称
  user_phone: String, // 用户电话
  user_img: String, // 用户图片
  // user_address: String, // 用户地址
  // user_id: String, // 用户id
  sid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "shop", // 商家id
  },
});

let OrderModel = mongoose.model("order", OrderChema, "order");
let ChatModel = mongoose.model("chat", ChatChema, "chat");
let ShopModel = mongoose.model("shops", ShopChema, "shops");
let UseModel = mongoose.model("use-shop", UseChema, "use-shop");

module.exports = {
  UserModule,
  TypeModel,
  BusinessModel,
  GoodModel,
  OrderModel,
  ChatModel,
  ShopModel,
  UseModel,
};
const shopSchema = new mongoose.Schema({
  name:String,
  distance:String,
  price:Number,
  num:Number,
  time:Number,
  buy:Boolean,
  type:String
})
const shopModel = mongoose.model('shop',shopSchema,'shop')

module.exports = {
 UserModule,TypeModel,BusinessModel,GoodModel,shopModel
}
